<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.User" %>
<%@ page import="com.bookmanager.model.BorrowRecord" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("../login.jsp");
        return;
    }

    // 从request属性获取真实数据
    Integer currentBorrows = (Integer) request.getAttribute("currentBorrows");
    Integer dueSoonBooks = (Integer) request.getAttribute("dueSoonBooks");
    Integer borrowHistory = (Integer) request.getAttribute("borrowHistory");
    Integer favoriteBooks = (Integer) request.getAttribute("favoriteBooks");

    // 获取真实的借阅记录
    List<BorrowRecord> currentBorrowRecords = (List<BorrowRecord>) request.getAttribute("currentBorrowRecords");

    // 设置默认值
    if (currentBorrows == null) currentBorrows = 0;
    if (dueSoonBooks == null) dueSoonBooks = 0;
    if (borrowHistory == null) borrowHistory = 0;
    if (favoriteBooks == null) favoriteBooks = 0;
%>
<html>
<head>
    <title>用户中心 - 图书管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 0 20px;
        }

        .nav-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            height: 70px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .welcome {
            color: #666;
        }

        .logout-btn {
            padding: 8px 16px;
            background: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 20px;
        }

        .dashboard-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .dashboard-header h1 {
            color: #333;
            margin-bottom: 10px;
        }

        .dashboard-header p {
            color: #666;
            font-size: 1.1rem;
        }

        .stats-cards {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }

        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            text-align: center;
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-icon {
            font-size: 2.5rem;
            margin-bottom: 15px;
        }

        .stat-number {
            font-size: 2rem;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 5px;
        }

        .stat-label {
            color: #666;
            font-size: 0.9rem;
        }

        .section {
            background: white;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .section-title {
            font-size: 1.3rem;
            color: #333;
        }

        .view-all {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .book-table {
            width: 100%;
            border-collapse: collapse;
        }

        .book-table th,
        .book-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #f0f0f0;
        }

        .book-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: #555;
        }

        .book-table tr:hover {
            background: #f8f9fa;
        }

        .status-overdue {
            color: #dc3545;
            font-weight: bold;
        }

        .status-borrowing {
            color: #28a745;
            font-weight: bold;
        }

        .btn {
            padding: 6px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.85rem;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-renew {
            background: #17a2b8;
            color: white;
        }

        .btn-detail {
            background: #6c757d;
            color: white;
        }

        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 30px;
        }

        .action-btn {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 15px 20px;
            background: white;
            border: 2px solid #667eea;
            border-radius: 8px;
            text-decoration: none;
            color: #667eea;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            background: #667eea;
            color: white;
        }

        .action-icon {
            font-size: 1.2rem;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #666;
        }

        .empty-icon {
            font-size: 3rem;
            margin-bottom: 15px;
            opacity: 0.5;
        }
        .btn-favorite {
            background: #e83e8c;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            font-size: 0.85rem;
        }

        .btn-favorite:hover {
            background: #d91a72;
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="user-info">
            <span class="welcome">欢迎，<strong><%= user.getUsername() %></strong></span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <!-- 仪表板头部 -->
    <div class="dashboard-header">
        <h1>用户中心</h1>
        <p>欢迎使用图书管理系统，在这里您可以管理您的借阅记录和图书查询</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
        <div class="stat-card">
            <div class="stat-icon">📖</div>
            <div class="stat-number"><%= currentBorrows %></div>
            <div class="stat-label">当前借阅</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">⏰</div>
            <div class="stat-number"><%= dueSoonBooks %></div>
            <div class="stat-label">即将到期</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">📚</div>
            <div class="stat-number"><%= borrowHistory %></div>
            <div class="stat-label">历史借阅</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">⭐</div>
            <div class="stat-number"><%= favoriteBooks %></div>
            <div class="stat-label">收藏图书</div>
        </div>
    </div>

    <!-- 快速操作 -->
    <div class="section">
        <h3 class="section-title">快速操作</h3>
        <div class="quick-actions">
            <a href="../books" class="action-btn">
                <span class="action-icon">🔍</span>
                <span>图书查询</span>
            </a>
            <a href="borrow-history" class="action-btn">
                <span class="action-icon">📊</span>
                <span>借阅历史</span>
            </a>
            <a href="../favorite" class="action-btn">
                <span class="action-icon">⭐</span>
                <span>我的收藏</span>
            </a>
            <a href="../profile" class="action-btn">
                <span class="action-icon">👤</span>
                <span>个人信息</span>
            </a>
        </div>
    </div>

    <!-- 当前借阅 -->
    <div class="section">
        <div class="section-header">
            <h3 class="section-title">当前借阅</h3>
            <a href="borrow-history.jsp" class="view-all">查看全部</a>
        </div>

        <% if (currentBorrows > 0 && currentBorrowRecords != null && !currentBorrowRecords.isEmpty()) { %>
        <table class="book-table">
            <thead>
            <tr>
                <th>图书名称</th>
                <th>作者</th>
                <th>借阅日期</th>
                <th>应还日期</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <% for (BorrowRecord record : currentBorrowRecords) { %>
            <tr>
                <td><%= record.getBookTitle() != null ? record.getBookTitle() : "未知图书" %></td>
                <td><%= record.getBookAuthor() != null ? record.getBookAuthor() : "未知作者" %></td>
                <td><%= record.getBorrowDate() != null ? record.getBorrowDate().toLocalDate().toString() : "未知" %></td>
                <td><%= record.getDueDate() != null ? record.getDueDate().toLocalDate().toString() : "未知" %></td>
                <td class="status-borrowing">
                    <% if (record.isOverdue()) { %>
                    已逾期
                    <% } else { %>
                    借阅中
                    <% } %>
                </td>
                <td>
                    <%
                        Map<Integer, Boolean> borrowRecordFavoriteStatus = (Map<Integer, Boolean>) request.getAttribute("borrowRecordFavoriteStatus");
                        Boolean isFavorite = false;
                        if (borrowRecordFavoriteStatus != null) {
                            Boolean favoriteStatus = borrowRecordFavoriteStatus.get(record.getBookId());
                            isFavorite = favoriteStatus != null ? favoriteStatus : false;
                        }
                    %>

                    <% if (!record.isOverdue()) { %>
                    <button class="btn btn-renew" onclick="renewBook(<%= record.getRecordId() %>)">续借</button>
                    <% } %>
                    <button class="btn btn-detail" onclick="showBookDetail(<%= record.getBookId() %>)">详情</button>
                    <button class="btn-favorite <%= isFavorite ? "favorited" : "" %>"
                            onclick="toggleFavorite(<%= record.getBookId() %>, <%= isFavorite %>)"
                            data-book-id="<%= record.getBookId() %>"
                            data-is-favorite="<%= isFavorite %>">
                        <%= isFavorite ? "★ 已收藏" : "☆ 收藏" %>
                    </button>
                </td>
            </tr>
            <% } %>
            </tbody>
        </table>
        <% } else { %>
        <div class="empty-state">
            <div class="empty-icon">📖</div>
            <h3>暂无借阅记录</h3>
            <p>快去借阅您喜欢的图书吧！</p>
            <a href="../books" class="btn" style="background: #667eea; color: white; padding: 10px 20px; margin-top: 15px;">去借书</a>
        </div>
        <% } %>
    </div>

    <!-- 推荐图书 -->
    <div class="section">
        <h3 class="section-title">为您推荐</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-top: 20px;">
            <div style="background: #f8f9fa; padding: 15px; border-radius: 8px; text-align: center;">
                <div style="font-size: 2rem; margin-bottom: 10px;">📘</div>
                <h4>数据库系统概念</h4>
                <p style="color: #666; font-size: 0.9rem;">Abraham Silberschatz</p>
                <button class="btn" style="background: #667eea; color: white; width: 100%; margin-top: 10px;">查看详情</button>
            </div>
            <div style="background: #f8f9fa; padding: 15px; border-radius: 8px; text-align: center;">
                <div style="font-size: 2rem; margin-bottom: 10px;">📗</div>
                <h4>设计模式</h4>
                <p style="color: #666; font-size: 0.9rem;">Erich Gamma</p>
                <button class="btn" style="background: #667eea; color: white; width: 100%; margin-top: 10px;">查看详情</button>
            </div>
        </div>
    </div>
</div>

<script>
    function toggleFavorite(bookId, isCurrentlyFavorite) {
        const action = isCurrentlyFavorite ? 'remove' : 'add';
        const confirmMessage = isCurrentlyFavorite ?
            '确定要取消收藏这本书吗？' : '确定要收藏这本书吗？';

        if (confirm(confirmMessage)) {
            // 创建表单提交收藏请求
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '../favorite';  // 注意路径，从用户中心到根目录

            const actionInput = document.createElement('input');
            actionInput.type = 'hidden';
            actionInput.name = 'action';
            actionInput.value = action;
            form.appendChild(actionInput);

            const bookIdInput = document.createElement('input');
            bookIdInput.type = 'hidden';
            bookIdInput.name = 'bookId';
            bookIdInput.value = bookId;
            form.appendChild(bookIdInput);

            document.body.appendChild(form);
            form.submit();
        }
    }
</script>
</body>
</html>